package com.shenfc.fxzj.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;

/**
 * Created by sfc on 2017/11/8
 */
public class QQUtils {

    private static Logger logger = LoggerFactory.getLogger(QQUtils.class);
    /**
     * QQappid
     */
    private static String appId;

    /**
     * QQappSecret
     */
    private static String appSecret;

    /**
     * 获取token Url
     */
    private static String accessTokenUrl;

    /**
     * 获取openId Url
     */
    private static String openIdUrl;


    public void setAppId(String appId) {
        this.appId = appId;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public void setAccessTokenUrl(String accessTokenUrl) {
        this.accessTokenUrl = accessTokenUrl;
    }

    public void setOpenIdUrl(String openIdUrl) {
        this.openIdUrl = openIdUrl;
    }

    /**
     * 获取QQ凭证
     *
     * @return token
     */
    public static String getAccessToken(String code) {
        String accessToken = "";
        //组装url
        String url = MessageFormat.format(accessTokenUrl, appId, appSecret, code, "http%3a%2f%2flocalhost%2flogin%2fqqCallback");
        String respStr = HttpClientImitator.doHttpResponse(url, false);
        if (respStr.indexOf("error") > 0) {
            logger.info("QQ获取token失败:" + respStr);
        } else {
            String[] split = respStr.split("&");
            accessToken = split[0].split("=")[1];
//            String expiresIn = split[1].split("=")[1];
            logger.info("QQ获取token成功:" + accessToken);
        }
        return accessToken;
    }

    /**
     * 获取openId
     *
     * @return openId
     */
    public static String getUserOpenId(String accessToken) {
        String openId = "";
        if (StringUtils.isBlank(accessToken)) {
            return openId;
        }
        String url = MessageFormat.format(openIdUrl, accessToken);
        /*请求api地址*/
        String respStr = HttpClientImitator.doHttpResponse(url, false);
        if (respStr.indexOf("error") > 0) {
            logger.info("QQ获取openId失败:" + respStr);
        } else {
            respStr = respStr.substring(respStr.indexOf("{"), respStr.indexOf("}") + 1);
            JSONObject jsonObject = JSON.parseObject(respStr);
            openId = jsonObject.get("openid").toString();
        }
        return openId;
    }

    /**
     * 获取openId
     *
     * @return openId
     */
    public static String getUserInfo(String accessToken, String openId) {
        String userInfo = "";
        if (StringUtils.isBlank(accessToken) || StringUtils.isBlank(openId)) {
            return userInfo;
        }
        String url = MessageFormat.format("https://graph.qq.com/user/get_user_info?access_token={0}&oauth_consumer_key={1}&openid={2}", accessToken, appId, openId);
        /*请求api地址*/
        String respStr = HttpClientImitator.doHttpResponse(url, false);
        JSONObject jsonObject = JSON.parseObject(respStr);
        String ret = jsonObject.get("ret").toString();
        if ("0".equals(ret)) {
            userInfo = respStr;
        } else {
            logger.info("QQ获取userInfo失败:" + respStr);
        }
        return userInfo;
    }

    public static void main(String[] args) throws Exception {

//        accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
//        appId = "wx19cfb79f3456d634";
//        appSecret = "f1f4059de001ee5caf4dcf36cdb1b5f6";
//        System.out.println(getAccessToken());

    }
}
